Repository navigation
Enter a GC free zone in shutdownLoop - #1
Conversation
The GC iterates the root set without locking, and only stops registered threads that are not in a GC free zone while doing so. - Work: destroy the RootedObject before UnregisterCurrentThread. - Timer and Async: leave the GC free zone in the uv_close callbacks, which run from uv_run inside runLoop's free zone, before removing the root. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
uv_run can block while work items are still running on libuv worker threads. If one of them triggers a collection, the collector waits for the blocked thread forever, like runLoop already avoids with its AutoGCFreeZone. This also makes the AutoGCZone in timer and async callbacks valid during shutdown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| uv_close(reinterpret_cast<uv_handle_t*>(async), [](uv_handle_t* handle) { | ||
| auto data = static_cast<hx::Object**>(handle->data); | ||
|
|
||
| // Removing the root touches GC memory, which must not happen from a GC free zone. |
There was a problem hiding this comment.
I'm not sure this comment is right (or the wording is very vague at least), seems like the core issue is that the root set mutex isn't acquired before the GC marks and visits the roots. I'm not sure if that was a conscious decision, but it sounds like it should acquire it, I can't immediately think of any reason why non GC threads shouldn't be able to create and remove roots.
There was a problem hiding this comment.
You're right, the comment was wrong. GCAddRoot/GCRemoveRoot take sGCRootLock, but MarkAll and the visit path iterate sgRootSet/sgOffsetRootSet without it. Leaving the free zone only worked around that by waiting out any running collection. I've opened HaxeFoundation/hxcpp#1438, which takes the lock while iterating, and dropped the root changes here, so this PR is now only the shutdownLoop free zone fix.
🤖 Generated with Claude Code
There was a problem hiding this comment.
Looks like we still need humans after all. At least for now...
The race they avoided comes from hxcpp walking the root set without taking sGCRootLock, so it's being fixed in hxcpp instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Found while looking into crashes and hangs with hxcoro's
LuvDispatcher(HaxeFoundation/hxcoro#164).runLoopwrapsuv_runin anAutoGCFreeZone, butshutdownLoopdidn't.uv_run(UV_RUN_DEFAULT)can block there waiting for work items that are still running on libuv workers. If one of those triggers a collection, the collector waits forever for the blocked thread, which isn't in a free zone, and the process hangs. A stack dump of a hung hxcoro test run showed exactly this. With the free zone added, there were no hangs in 200 runs, against about 2% before. The free zone also makes theAutoGCZonein timer and async callbacks valid when they run during shutdown.An earlier version of this PR also moved GC root removals out of free zones. Those changes only worked around hxcpp iterating the root set without its lock, so they've been dropped. That's fixed in HaxeFoundation/hxcpp#1438 instead.
Work.queuealso registers and unregisters the worker thread for every work item. Registering each libuv worker once and having it wait in a GC-free zone between items would avoid that cost. I haven't made that change here.🤖 Generated with Claude Code